package org.nanotek.tika.fs;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;

import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.tika.io.TikaInputStream;
import org.nanotek.cms.domain.lucene.IndexHolder;
import org.nanotek.cms.domain.lucene.config.Index;
import org.nanotek.tika.io.TikaFileVisitor;
import org.nanotek.tika.io.TikaPersistentFileVisitor;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DirectoryScanner implements ApplicationContextAware{
	
	private String [] rootDirs; 
    /** The patterns for the files to be included. */
    protected String[] includes;
	private ApplicationContext applicationContext;
    
	public DirectoryScanner() {
	}

	public String[] getRootDirs() {
		return rootDirs;
	}

	public void setRootDirs(String[] rootDirs) {
		this.rootDirs = rootDirs;
	}
	
	public String[] getIncludes() {
		return includes;
	}

	public void setIncludes(String[] includes) {
		this.includes = includes;
	}
	
	public void scan () throws IOException 
	{ 
		assert (applicationContext !=null);
		String pattern = null;
//		LuceneIndexWriteService<TikaInputStream> indexWriterService = (LuceneIndexWriteService<TikaInputStream>) applicationContext.getBean("luceneIndexService");
//		indexWriterService.initService(indexLocation);
		///////
		TikaFileVisitor finder = (TikaFileVisitor) applicationContext.getBean("tikaFileVisitor"); 
//		finder.setIndexWriterService(indexWriterService);
		//////
//		IndexWriter indexWriter = finder.getIndexWriterService().getIndexWriter();
//		Directory directory = indexWriter.getDirectory(); 
		for (String include : includes)
		{ 
			for (String rootDir : rootDirs) { 
				Path inputPath = new File(rootDir).toPath();
				pattern = include;
				finder.setMatcher(FileSystems.getDefault().getPathMatcher("glob:" + pattern));
//				TikaPersistentFileVisitor finder = new TikaPersistentFileVisitor(pattern);
		        Files.walkFileTree(inputPath, finder);
		        finder.done();
			}
		}
//		if (indexWriterService !=null){ 
//			indexWriterService.shutDownService();
//		}
	}
	
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {
		"base-local-tika-config/tika-base-context.xml"});

			Long startTime = new Date().getTime();
			DirectoryScanner dirsCan = (DirectoryScanner) ctx.getBean("baseDirectoryScanner");
//			dirsCan.setRootDirs(new String[]{"C:\\"});
			dirsCan.setIncludes(new String[]{"*{.java,class}"});
	        dirsCan.scan();
	        Long duration = (new Date().getTime() - startTime)/1000; 
	        System.err.println("Time in seconds to scan: " + duration);
	    }

	@Override
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		this.applicationContext = arg0;
	}
}
